package com.lv.service.repo

import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * cniao数据库操作帮助类
 */
object HomeSearchDbHelper {

    /**
     * 获取room数据表中存储的userInfo
     * return liveData形式
     */
    fun getLiveHomeSearch(context: Context, search: String) =
        HomeSearchDatabase.getInstance(context).homeSearchDao.queryLiveHomeSearch(search)

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    fun getHomeSearch(context: Context, search: String) =
        HomeSearchDatabase.getInstance(context).homeSearchDao.queryHomeSearch(search)

    /**
     * 删除数据表中的userInfo信息
     */
    fun deleteHomeSearch(context: Context, search: String) {
        GlobalScope.launch(Dispatchers.IO) {
            getHomeSearch(context, search = search)?.let { info ->
                HomeSearchDatabase.getInstance(context).homeSearchDao.deleteHomeSearch(info)
            }
        }
    }

    /**
     * 删除数据表中的userInfo信息
     */
    fun deleteAllHomeSearch(context: Context) {
        GlobalScope.launch(Dispatchers.IO) {
            HomeSearchDatabase.getInstance(context).homeSearchDao.deleteAllHomeSearch()

        }
    }

    /**
     * 新增用户数据到数据表
     */
    fun insertHomeSearch(context: Context, user: HomeSearch) {
        GlobalScope.launch(Dispatchers.IO) {
            HomeSearchDatabase.getInstance(context).homeSearchDao.insertHomeSearch(user)
        }
    }

    /**
     * 获取的所有用户数据到数据表
     */
    suspend fun getAllHomeSearch(context: Context) =
        HomeSearchDatabase.getInstance(context).homeSearchDao.getAllHomeSearch()


}